package com.crystal.jcms.shpz;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.crystal.constant.Constant;
import com.crystal.jcms.ui.CustomTitleActivity;
import com.crystal.jcms.utils.DialogUtils;
import com.crystal.jcms.utils.ShowMsgUtils;
import com.crystal.jcms.utils.SpUtils;
import com.crystal.jcms.utils.RequestUtils;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class ActivityMain extends CustomTitleActivity {
	private Context mContext;
	private ArrayList<Map<String, String>> list;
	private SimpleAdapter adapter;
	private ListView listview;
	private String flag = "", Select_Nbzlh = "", Select_ItemName = "", dbType = "";
	public Dialog mLoading;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		flag = getIntent().getStringExtra("Flag");
		dbType = getIntent().getStringExtra("dbType");
		setTitle(flag);
		showBackwardView(R.string.text_back, true);
		setContentView(R.layout.activity_main);
		initData();
		listview = (ListView) findViewById(R.id.listview);
		listview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Select_Nbzlh = list.get(position).get("Nbzlh");
				Select_ItemName = list.get(position).get("ItemName");
				Message message = new Message();
				message.what = Constant.SHOWPAGE;
				handler.sendMessage(message);
			}
		});
	}

	private void initData() {
		mLoading = DialogUtils.createLoadingDialog(mContext, "加载中...");
		mLoading.show();
		list = new ArrayList<Map<String, String>>();
		Map<String, String> params = new HashMap<String, String>();
		params.put("type", flag);
		params.put("userId", (String) SpUtils.get(mContext, "UserId", ""));
		params.put("dbType", dbType);
		RequestUtils.doPostAsyn(mContext, "main.xml", "GetToDoWorkList", params, new RequestUtils.CallBack() {

			@Override
			public void onRequestComplete(String result) {
				Message message = new Message();
				message.what = Constant.SHOWTOAST;
				try {
					if (result.contains("500_")) {
						message.obj = result.substring(4);
						handler.sendMessage(message);
						return;
					}
					JSONObject jsonResult = new JSONObject(result);
					if (!jsonResult.get("Result").equals("")) {
						JSONArray jsonArray = jsonResult.getJSONArray("Result");
						for (int i = 0; i < jsonArray.length(); i++) {
							JSONObject object = jsonArray.getJSONObject(i);
							Map<String, String> item = new HashMap<String, String>();
							item.put("Nbzlh", object.getString("Nbzlh"));
							item.put("ItemName", object.getString("ItemName"));
							item.put("TodoWorkCount", object.getString("TodoWorkCount"));
							list.add(item);
						}
						message.what = Constant.BINDLISTVIEW;
					} else {
						message.obj = "暂无数据!";
					}
				} catch (JSONException e) {
					e.printStackTrace();
					message.obj = e.getMessage();
				}
				handler.sendMessage(message);
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case Constant.CALLBACKCODE:
			initData();
			break;
		}
	}

	@SuppressLint("HandlerLeak")
	public Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (mLoading != null) {
				if (mLoading.isShowing()) {
					mLoading.dismiss();
				}
			}
			Intent intent = new Intent(mContext, ActivityList.class);
			switch (msg.what) {
			case Constant.SHOWPAGE:
				intent.putExtra("Flag", flag);
				intent.putExtra("Nbzlh", Select_Nbzlh);
				intent.putExtra("ItemName", Select_ItemName);
				intent.putExtra("dbType", dbType);
				startActivityForResult(intent, Constant.CALLBACKCODE);
				break;
			case Constant.BINDLISTVIEW:
				adapter = new SimpleAdapter(mContext, list, R.layout.listview_item_main,
						new String[] { "ItemName", "TodoWorkCount" }, new int[] { R.id.txtItemName, R.id.txtToDoWork });
				listview.setAdapter(adapter);
				adapter.notifyDataSetChanged();
				break;
			case Constant.SHOWTOAST:
				ShowMsgUtils.ShowToast(mContext, (String) msg.obj);
				break;
			case Constant.SHOWMESSAGE:
				ShowMsgUtils.ShowMessage(mContext, (String) msg.obj);
				break;
			}
		}
	};

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
}
